/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package motiveai.tests;

import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;

/**
 *
 * @author oxplay
 */
public class MoveControl extends AbstractControl {

    private Vector3f vector = new Vector3f(0, 0, 0);
    private float speed = 3f;
    private float stopDistance = 0.05f;

    public float getStopDistance() {
        return stopDistance;
    }

    public void setStopDistance(float stopDistance) {
        this.stopDistance = stopDistance;
    }
    
    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    @Override
    public void setSpatial(Spatial spatial) {
        this.vector = spatial.getLocalTranslation();
        this.spatial = spatial;
    }

    public void moveTo(Vector3f moveVector) {
        this.vector = moveVector;
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (spatial != null) {
            Vector3f actualPosition = spatial.getLocalTranslation();
            if (!vector.equals(actualPosition)) {
                Vector3f directionVector = vector.add(actualPosition.negate());
                Vector3f newPosition = actualPosition.add(directionVector.normalize().mult(tpf*speed));
                if(stopDistance >= spatial.getLocalTranslation().distance(vector)){
                    spatial.setLocalTranslation(vector);
                } else {
                    spatial.setLocalTranslation(newPosition);
                }
            }
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
